<?php
namespace app\client\controller;
use app\common\lib\Redis;
use think\swoole\facade\Task;

class Visit extends Base {
    public function __construct() {
        parent::__construct();
    }
    
    public function user() {
        $userId = request()->get('userId', 0, 'intval');
        if($userId <= 0) {
            return errorResult();
        }
        $user = model('User')->findOne(['user_id' => $userId], ['nickname', 'avatar', 'remark', 'background']);
        return successResult($user);
    }
    
    //用户主页
    public function homepage() {
        $userId = request()->get('userId', 0, 'intval');
        if($userId <= 0) {
            return errorResult();
        }
        $redis = new Redis();
        $user = $redis->get('user_' . $userId);
        if(empty($user)) {
            return errorResult();
        }
        
        $repdata = ['user' => $user, 'picture' => []];
        
        //获取5个最新的动态
        $rows = model('Chat')->where(['user_id' => $userId])->field('cover')->order('chat_id desc')->limit(0, 5)->select();
        foreach ($rows as $row) {
            if(!empty($row['cover'])) {
                array_push($repdata['picture'], $row['cover']);
            }
        }
        
        $repdata['praise_number'] = $redis->get(redisUserChatPraiseKey($userId));
        
        //判断有无关注
        $repdata['followed'] = model('FollowUser')->isFollowed($this->userid, $userId);
        
        //判断是不是代理
        $repdata['is_agent'] = model('Agent')->isAgent($this->userid, $userId);
        
        return successResult($repdata);
    }
    
    //关注某个用户
    public function follow() {
        $userId = request()->post('userId', 0, 'intval');
        if($userId <= 0) {
            return errorResult('dodo');
        }
        $myUserId = $this->userid;
        
        if($userId === $myUserId) {
            return errorResult('你无时无刻不在关注自己');
        }
        
        $user = model('User')->isExistsByPk($userId);
        if(!$user){
            return errorResult('ccc');
        }
        $followModel = model('FollowUser');
        Task::async(function() use($userId, $myUserId, &$followModel){
            $redis = new Redis();
            try {
                
                $time = time();
                
                //判断是否已经关注
                $isFollow = $followModel->isFollowed($myUserId, $userId);
                if(!$isFollow){
                    $followModel->add(['user_id' => $userId, 'follow_user_id' => $myUserId]); //加入关注表
                    
                    $redis->incrBy('fans_' . $userId); //粉丝人数
                    
                }
                
            }catch (\Exception $e) {
            }
            
            $redis->close();
            
            return true;
        });
        
        return successResult('关注成功');
    }
    
    //取消关注
    public function unfollow() {
        $userId = request()->post('userId', 0, 'intval');
        if($userId <= 0) {
            return errorResult();
        }
        $myUserId = $this->userid;
        
        if($userId === $myUserId) {
            return errorResult('你无时无刻不在关注自己');
        }
        
        $user = model('User')->isExistsByPk($userId);
        if(!$user){
            return errorResult();
        }
        
        $followModel = model('FollowUser');
        Task::async(function() use($userId, $myUserId, &$followModel){
            $redis = new Redis();
            try {
                //判断是否已经关注
                $follow = $followModel->findOne(['follow_user_id' => $myUserId, 'user_id' => $userId], 'id');
                if(!empty($follow)) {
                    $followModel->where(['id' => $follow['id']])->delete();
                    $redis->incrBy('fans_' . $userId, -1); //粉丝人数
                }
                
            }catch (\Exception $e) {
            }
            
            $redis->close();
            
            return true;
        });
            
        return successResult('关注成功');
    }
    
    public function contactInfo() {
        $userId = request()->get('userId', 0, 'intval');
        if($userId <= 0) {
            return errorResult();
        }
        $userInfo = model('User')->findOne(['user_id' => $userId], ['avatar', 'nickname', 'mobile', 'wechat']);
        return successResult($userInfo);
    }
}











